package com.mashibing.helloworld;

import com.mashibing.util.RabbitMQConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

/**
 * Hello World方式的Publisher
 * @author zjw
 */
public class Send {

    private final static String QUEUE_NAME = "hello";

    @Test
    public void publish() throws Exception {
        //1. 获取链接对象
        Connection connection = RabbitMQConnectionUtil.getConnection();

        //2. 构建通道
        Channel channel = connection.openChannel().get();

        //3. 交换机提供了，队列需要手动创建
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        //4. 发送消息到交换机
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

        System.out.println(" [x] Sent '" + message + "'");
    }
}
